package com.wwwarehouse.contract.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.contract.bean.ChooseModuleBean;
import com.wwwarehouse.contract.bean.PinCardBean;
import com.wwwarehouse.contract.bean.SupplyOfResourcesBean;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.ArrayList;
import java.util.List;

import contract.wwwarehouse.com.contract.R;

import static com.yolanda.nohttp.tools.ResCompat.getColor;

/**
 * Created by feng.gao on 2017/9/22.
 */

public class SupplyOfResourcesAdapter extends BaseAdapter {
	private List<ChooseModuleBean.ModuleList.BeanList> list = new ArrayList<>();
	private Context context;
	private boolean isFunction;
	private checkedListener mOnCheckedListener;
	public SupplyOfResourcesAdapter(List<ChooseModuleBean.ModuleList.BeanList> list, Context context , boolean isFunction) {
		this.list = list;
		this.context = context;
		this.isFunction = isFunction;
	}

	public void setOncheckListener(checkedListener mOnCheckedListener) {
		this.mOnCheckedListener = mOnCheckedListener;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.supply_of_resources_item, null);
			viewHolder = new ViewHolder();
			viewHolder.supplyIv = (ImageView) convertView.findViewById(R.id.supply_resource_iv);
			viewHolder.supplyTv = (TextView) convertView.findViewById(R.id.supply_resource_tv);
			viewHolder.mContentLayout = (LinearLayout) convertView.findViewById(R.id.content_layout);
			viewHolder.mUncheckedLayout = (LinearLayout) convertView.findViewById(R.id.un_checked_layout);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		if (list != null && list.size()>0) {
			viewHolder.supplyTv.setText(list.get(position).getName());
			if (list.get(position).isSelect()) {
				if (list.get(position).getSelected() == 1 && isFunction) {
					viewHolder.mContentLayout.setBackground(context.getResources().getDrawable(R.drawable.circle_gray_rescource_bg));
					viewHolder.mUncheckedLayout.setVisibility(View.VISIBLE);
					viewHolder.supplyTv.setTextColor(context.getResources().getColor(R.color.common_color_c7_96999e));

				} else {
					viewHolder.mUncheckedLayout.setVisibility(View.GONE);
							viewHolder.mContentLayout.setBackground(context.getResources().getDrawable(R.drawable.blue_circle_eight_five_bg));
							viewHolder.supplyTv.setTextColor(context.getResources().getColor(R.color.common_color_c3_ffffff));

				}
				if (list.get(position).getSelected() == 1 && isFunction) {
					if (list.get(position).getImageUrl() != null && list.get(position).getImageUrl().getDefaultUrl() != null) {
						BaseApplication.getApplicationInstance().displayImg(list.get(position).getImageUrl().getDefaultUrl(), viewHolder.supplyIv);
					}
				} else {
					if (list.get(position).getImageUrl() != null && list.get(position).getImageUrl().getSelectedUrl() != null) {
						BaseApplication.getApplicationInstance().displayImg(list.get(position).getImageUrl().getSelectedUrl(), viewHolder.supplyIv);
					}
				}

			} else {
				if (list.get(position).getSelected() == 1 && isFunction) {
					viewHolder.mContentLayout.setBackground(context.getResources().getDrawable(R.drawable.circle_gray_rescource_bg));
					viewHolder.supplyTv.setTextColor(context.getResources().getColor(R.color.common_color_c7_96999e));
					viewHolder.mUncheckedLayout.setVisibility(View.VISIBLE);
					if (list.get(position).getImageUrl() != null && list.get(position).getImageUrl().getDefaultUrl() != null) {
						BaseApplication.getApplicationInstance().displayImg(list.get(position).getImageUrl().getDefaultUrl(), viewHolder.supplyIv);
					}
					return convertView ;

				}
				viewHolder.mContentLayout.setBackground(context.getResources().getDrawable(R.drawable.circle_gray_rescource_bg));
				viewHolder.supplyTv.setTextColor(context.getResources().getColor(R.color.common_color_c7_96999e));
				if (list.get(position).getImageUrl() != null &&list.get(position).getImageUrl().getDefaultUrl() != null) {
					BaseApplication.getApplicationInstance().displayImg(list.get(position).getImageUrl().getDefaultUrl(), viewHolder.supplyIv);
				}

			}
			viewHolder.mContentLayout.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (list.get(position).getSelected() == 1 && isFunction) {
						return;
					}
					if (list.get(position).isSelect()) {
						if (list.get(position).getSelected() == 1 && isFunction) {
							viewHolder.mContentLayout.setBackground(context.getResources().getDrawable(R.drawable.circle_gray_rescource_bg));
							viewHolder.mUncheckedLayout.setVisibility(View.VISIBLE);
							viewHolder.supplyTv.setTextColor(context.getResources().getColor(R.color.common_color_c7_96999e));

						} else {
							viewHolder.mContentLayout.setBackground(context.getResources().getDrawable(R.drawable.circle_gray_rescource_bg));
							viewHolder.supplyTv.setTextColor(context.getResources().getColor(R.color.common_color_c7_96999e));
							viewHolder.mUncheckedLayout.setVisibility(View.GONE);
//							viewHolder.mContentLayout.setBackground(context.getResources().getDrawable(R.drawable.blue_circle_eight_five_bg));
//							viewHolder.supplyTv.setTextColor(context.getResources().getColor(R.color.common_color_c3_ffffff));

						}
						if (list.get(position).getSelected() == 1 && isFunction) {
							if (list.get(position).getImageUrl() != null && list.get(position).getImageUrl().getDefaultUrl() != null) {
								BaseApplication.getApplicationInstance().displayImg(list.get(position).getImageUrl().getDefaultUrl(), viewHolder.supplyIv);
							}
						} else {
							if (list.get(position).getImageUrl() != null && list.get(position).getImageUrl().getDefaultUrl() != null) {
								BaseApplication.getApplicationInstance().displayImg(list.get(position).getImageUrl().getDefaultUrl(), viewHolder.supplyIv);
							}
						}
						list.get(position).setSelect(false);

					} else {
						if (list.get(position).getSelected() == 1 && isFunction) {
							viewHolder.mContentLayout.setBackground(context.getResources().getDrawable(R.drawable.circle_gray_rescource_bg));
							viewHolder.mUncheckedLayout.setVisibility(View.VISIBLE);
							viewHolder.supplyTv.setTextColor(context.getResources().getColor(R.color.common_color_c7_96999e));
							if (list.get(position).getImageUrl() != null && list.get(position).getImageUrl().getDefaultUrl() != null) {
								BaseApplication.getApplicationInstance().displayImg(list.get(position).getImageUrl().getDefaultUrl(), viewHolder.supplyIv);
							}
							return ;

						}
						viewHolder.mContentLayout.setBackground(context.getResources().getDrawable(R.drawable.blue_circle_eight_five_bg));
							viewHolder.supplyTv.setTextColor(context.getResources().getColor(R.color.common_color_c3_ffffff));
						if (list.get(position).getImageUrl() != null &&list.get(position).getImageUrl().getSelectedUrl() != null) {
							BaseApplication.getApplicationInstance().displayImg(list.get(position).getImageUrl().getSelectedUrl(), viewHolder.supplyIv);
						}
						list.get(position).setSelect(true);

					}

					mOnCheckedListener.onCheckedListener(list.get(position));

				}
			});

//			viewHolder.client_tv.setText(list.get(position).getTitle());

//				BaseApplication.getApplicationInstance().displayImg(list.get(position).getGrayUrl() , viewHolder.supplyIv);

		}
		return convertView;
	}

	class ViewHolder {
		TextView supplyTv;
		ImageView supplyIv;
		LinearLayout mContentLayout;
		LinearLayout mUncheckedLayout;

	}

	public interface checkedListener {
		void onCheckedListener(ChooseModuleBean.ModuleList.BeanList lists);
	}



}